<?php
include("config.php");
//提交验证码后

if($_POST['UserID']&&$_POST['PassWord']&&$_POST['cCode']&&$_POST['session_id']&&$_POST['user_type']){
	
	//模拟登录
	$snoopy = new Snoopy; 
	$submit_url = $loginurl;
	//定义POST内容
	$submit_vars["UserID"] = $_POST['UserID'];
	$submit_vars["typeName"] = "";
	if( $_POST['user_type'] == 'STU' ){
		$submit_vars["Sel_Type"] = "STU";	//学生为STU,教师为TEA
	}else{ 
		$submit_vars["Sel_Type"] = "TEA";
	}
	$submit_vars["sbtState"] = "";
	$submit_vars["pcInfo"] = "";
	$submit_vars["PassWord"] = $_POST['PassWord'];
	$submit_vars["cCode"] = $_POST['cCode'];
	$submit_vars["__VIEWSTATE"] = $_SESSION['VIEWSTATE'];
	//var_dump($_SESSION['VIEWSTATE']);
	//模拟HEADER
	$snoopy->agent = $agent;
	$snoopy->host = IP;
	$snoopy->referer = $submit_url;
	$snoopy->cookies['ASP.NET_SessionId'] = $_SESSION["ASP_NET_SessionId"];
	$snoopy->cookies['sVC'] = $_SESSION["sVC"];
	//提交
	$snoopy->submit($submit_url,$submit_vars);

	//超时返回
	if ($snoopy->status != '200' || $snoopy->timed_out) {
		echo 1;
    	echo json_err(1,"登录提交超时！");
    	exit;
	}
	
	
	//转码以便查找错误
	$snoopy_results = mb_convert_encoding($snoopy->results, 'utf-8', 'gbk');
	
	/*
	echo json_encode($snoopy_results);
	if( $snoopy_results == "" ){ 
		echo json_err(7,"empty");
		break;
	}
	*/


	if(stripos($snoopy_results,"密码不正确！")){
    	echo json_err(3,"用户名或密码错误！");
    	exit;
	}
	if(stripos($snoopy_results,"验证码错误！")){

    	echo json_err(4,"验证码错误！");
    	exit;
	}
	if(stripos($snoopy_results,"页面错误")){
    	echo json_err(5,"session_id错误或超时！");
    	exit;
	}
	if(stripos($snoopy_results,"数据库连接失败！")){
    	echo json_err(5,"session_id错误或超时！");
    	exit;
	}

	//如果只验证登录信息，不进行登录后的其他操作
	if(!$login_do){
		//noapi调用
		if($_POST['noapi']){
			echo "登录成功！";
			return;
		}
		echo json_err(0,'登录成功!');
		return;
	}
	//mysql_close($link);
}else{
	echo json_err(2,'提交数据不全!');
	break;
}

?>
